//
//
// @author Michael Liljenstam, ISTS, Dartmouth College
//
// $Id: UniformWormEpidemicInitializer.java,v 1.2 2003/03/01 21:16:58 mili Exp mili $

package SSF.App.Worm;

import java.util.*;


/**
 *  Initialize worm epidemic with a 'uniform distribution', i.e.  
 *  (i) uniform distribution of susceptible hosts over leaf ASes, and 
 *  (ii) uniform distribution of IP addresses (cross-strata infection 
 *  parameters).
 */
public class UniformWormEpidemicInitializer extends WormEpidemicInitializer {

    /** 
     *  Perform the initialization.
     */
    public void doInit() {
	ASGraph asGraph = MacroscopicModel.ref.net;
	int numASs = asGraph.numASs;

	float Beta = MacroscopicModel.ref.wormModel.Beta;
	float sTot = MacroscopicModel.ref.wormModel.sTot;
	float iTot = MacroscopicModel.ref.wormModel.iTot;
	float rTot = MacroscopicModel.ref.wormModel.rTot;

	// init infection parameters
	float[] BetaJ  = new float[numASs];
	MacroscopicModel.ref.wormModel.BetaJ = BetaJ;
	for (int j = 0; j < numASs; j++) {
	    // set all BetaJ = Beta
	    BetaJ[j] = Beta;
	} // for

	// init susceptibles
	LinkedList leaves = new LinkedList();
	if (numASs > 1) {
	    // stratified model
	    for (int j = 0; j < numASs; j++) {
		if (asGraph.ases[j].adjacencyList.size() == 1)
		    leaves.add(asGraph.ases[j]);
	    }
	} else {
	    // homogeneous model
	    leaves.add(asGraph.ases[0]);
	}
	int numLeaves = leaves.size();
	for (int i = 0; i < numLeaves; i++) {
	    AS as = (AS) leaves.removeFirst();
	    as.wormModel.s = sTot / numLeaves; // Uniform distr over leaves
	}

	// init initially infected	    
	asGraph.ases[0].wormModel.i = iTot; // all initally infected in AS 0

	// local N (s_0 + i_0)
	for (int j = 0; j < numASs; j++) {
	    AS n = asGraph.ases[j];
	    n.wormModel.N = 
		(int) Math.ceil(n.wormModel.s + n.wormModel.i);
	}

	if (MacroscopicModel.ref.wormModel.DEBUG) {
	    System.out.println("Init worm epidemic model: s=" 
			       + sTot + ", i=" + iTot + ", r=" + rTot
			       + ", Beta=" + Beta);
	}
    } // doInit()
}
